<?php

/**
 * @author     bigface.cc
 * @date       2012-7-8 
 * @version    1.0
 */
//查茶网分类控制器,支持二级分类，要开启无限分类只需需改添加分类时候的，让二级分类也显示出来即可
//删除的时候没有删除无限分类，只有删除到二级分类 2012-7-9  大脸
class links_typeAction extends baseAction {

        //显示分类列表
        function index() {
                $type_mod=M('links_type');
                $result=$type_mod->order('sort DESC')->select();
                foreach ($result as $val) {
                        if($val['pid']==0){
                                $type_list['parent'][$val['id']]=$val;
                        }else{
                                $type_list['sub'][$val['pid']][]=$val;
                        }
                }
                $this->assign('type_list',$type_list);
//                dump($type_list);exit;
                $this->display();
        }

        //添加分类
        function add() {
                $type_mod=M('links_type');
                if(isset ($_POST['dosubmit'])){
                        if(FALSE===$data=$type_mod->create()){
                                $this->error($type_mod->error());
                        }
                        if($data['type_name']==''){
                                $this->error("分类名称不能为空");
                        }
                        //检测分类是否存在
                        $result=$type_mod->where('type_name='.$data['type_name'].' AND pid='.$data['pid'])->count();
                        if($result!=0){
                                $this->error("该分类已经存在");
                        }
                        $rs=$type_mod->add($data);
                        if($rs){
                                $this->success('添加分类成功', U('links_type/index'), 1);
                        }
                }else{
                        $result=$type_mod->order('sort DESC')->select();
                        foreach ($result as $val) {
                                if($val['pid']==0){
                                        $type_list['parent'][$val['id']]=$val;
                                }else{
                                        $type_list['sub'][$val['pid']][]=$val;
                                }
                        }
                        $this->assign('type_list',$type_list);
                }
                $this->display();
        }

        //修改分类
        function edit() {
                $type_mod=M('links_type');
                if (isset ($_POST['dosubmit'])) {
                        $data=$type_mod->create();
                        $result=$type_mod->where("id=" . $data['id'])->save($data);
                        if(FALSE!==$result){
                                $this->success('修改成功',U('links_type/index'),1);
                        }else{
                                $this->error(L('operation_failure'));
                        }
                }  else {
                        if(isset ($_GET['id'])){
                                $type_id=isset ($_GET['id']) && intval($_GET['id'])?intval($_GET['id']):$this->error(L('please_select').'要编辑的分类');
                        }
                        $result=$type_mod->order('sort DESC')->select();
                        foreach ($result as $val) {
                                if($val['pid']==0){
                                        $type_list['parent'][$val['id']]=$val;
                                }else{
                                        $type_list['sub'][$val['pid']][]=$val;
                                }
                        }
                        $this->assign('type_list',$type_list);
                        $type_info=$type_mod->where('id='.$type_id)->find();
                        $this->assign("type_info",$type_info);
                }
                $this->display();
        }
        function del(){
                if((!isset ($_GET['id']) || empty ($_GET['id'])) && (!isset ($_POST['id']) || empty ($_POST['id']))){
                        $this->error("请选择你要删除的分类");
                }
                $type_mod=M('links_type');
                if(isset ($_POST['id']) && is_array($_POST['id'])){
                        $type_mod->delete(implode(',', $_POST['id']));
                }else{
                        $type_mod->delete($_POST['id']);
                }
                $this->success(L('operation_success'));
        }
        //隐藏/显示分类
        function hide(){
                $type_mod=M('links_type');
                $id = intval($_REQUEST['id']);
                $type = trim($_REQUEST['type']);
                $sql="UPDATE ".C('DB_PREFIX')."links_type SET $type=($type+1)%2 WHERE id = $id";
                $rs=$type_mod->execute($sql);
//                $this->ajaxReturn($sql);exit;
                $values=$type_mod->field($type)->where('id='.$id)->find();
                $this->ajaxReturn($values[$type]);
        }
        //分类排序 数字越大越靠前
        function sort(){
                $type_mod=M('links_type');
                if(isset ($_POST['listorders'])){
                        foreach ($_POST['listorders'] as $id => $sort) {
                                $data['sort']=$sort;
                                $type_mod->where('id='.$id)->save($data);
                        }
                        $this->success(L('operation_success'));
                }else{
                        $this->error(L('operation_failure'));
                }
        }
}

?>
